package cn.yunhe.biz.impl;

import java.util.List;

import javax.swing.MenuElement;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.yunhe.biz.IMenuBiz;
import cn.yunhe.dao.MenuMapper;
import cn.yunhe.entity.Menu;
import cn.yunhe.entity.MenuExample;
@Service("IMBiz")
public class MenuBizImpl implements IMenuBiz {

	@Autowired
	private MenuMapper menumapper;

	public void setMenumapper(MenuMapper menumapper) {
		this.menumapper = menumapper;
	}
	
	
	@Override
	public List<Menu> FindAllMenu(String mName) {
		// TODO Auto-generated method stub
		List<Menu> list = null;
		try {
			MenuExample example = new MenuExample();
			MenuExample.Criteria crita = example.createCriteria();
			if(StringUtils.isNotBlank(mName)){
				mName = "%"+ mName+"%";
			}
			if(StringUtils.isNotBlank(mName)){
				crita.andMNameLike(mName);
			}
			list = menumapper.selectByExample(example);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}
	
	
	@Override
	public Integer AddMenu(Menu menu) {
		// TODO Auto-generated method stub
		Integer flag = 0;
		try {
			flag = menumapper.insert(menu);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}


	@Override
	public Menu FindMenuById(Integer mId) {
		// TODO Auto-generated method stub
		List<Menu> list = null;
		try {
			MenuExample example = new MenuExample();
			MenuExample.Criteria crita = example.createCriteria();
			crita.andMIdEqualTo(mId);
			list = menumapper.selectByExample(example);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list.get(0);
	}


	@Override
	public Integer UpdateMenuById(Menu menu, Integer mId) {
		// TODO Auto-generated method stub
		Integer flag = 0;
		try {
			MenuExample example = new MenuExample();
			MenuExample.Criteria crita = example.createCriteria();
			crita.andMIdEqualTo(mId);
			flag = menumapper.updateByExample(menu, example);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return flag;
	}


	@Override
	public boolean DelMenuById(Integer mId) {
		// TODO Auto-generated method stub
		Integer flag = 0;
		try {
			MenuExample example = new MenuExample();
			MenuExample.Criteria crita = example.createCriteria();
			crita.andMIdEqualTo(mId);
			flag = menumapper.deleteByExample(example);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(flag>0){
			return true;
		}else{
			return false;
		}
	}
	
}
